// pages/visualization/visualization.ts
var wxCharts = require('../../utils/wxcharts');
var app = getApp();
var pieChart = null;
var lineChart = null;
var pieData = [];
var lineData = [];
Page({
  touchHandlerpieCanvas: function (e: any) {
    console.log(pieChart.getCurrentDataIndex(e));
  },
  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: 320,
    tabs: [],
    activeTab: 0,
    // timeforpie: "",
    slotforpie: 1,
    startDate: '2021-01-01',
    endDate: '2021-07-01',
    array: ["ALL", "SLOT1", "SLOT2", "SLOT3", "SLOT4", "SLOT5", "SLOT6", "SLOT7", "SLOT8"],
    index: 1,
    
  },


  bindStartDateChange: function (e: any) {
    console.log('starttime发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDate: e.detail.value
    })
  },


  bindEndDateChange: function (e: any) {
    console.log('endtime发送选择改变，携带值为', e.detail.value)
    this.setData({
      startDate: e.detail.value
    })
  },


  bindSlotChange: function (e: any) {
    console.log('slot发送选择改变，携带值为', e.detail.value)
    this.setData({
      slotforpie: e.detail.value
    })
  },
  moveHandler() {

  },

  inputValue(e: any) {
    this.data.slotforpie = e.detail.value
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    try {
      var res = wx.getSystemInfoSync();
      this.data.windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    const tabs = [
      {
        title: '   PIE  ',
        title2: 'Goods distrubtion',
        desc: '',
        canvas: "pieCanvas",
      },
      {
        title: '流动情况',
        title2: '微信小程序直播',
        desc: '',
        canvas: "areaCanvas",
      },
      {
        title: '   LINE   ',
        title2: 'changing situation',
        desc: '',
        canvas: "lineCanvas",
      },
      {
        title: '营销经验',
        title2: '流量主小程序',
        desc: '',
      },

    ]

    pieData = [{
      name: '成交量1',
      data: 15,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量4',
      data: 63,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量4',
      data: 63,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量3',
      data: 78,
    }]

    lineData = [{
      name: 'SLOT1',
      data: [12, 4, 5, 8, 10, 4, 7, 4, 2, 1],
      format: function (val, name) {
        return val.toFixed(2);
      }
    }, {
      name: 'SLOT2',
      data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
      format: function (val, name) {
        return val.toFixed(2);
      }
    }]

    this.setData({ tabs })


    var categories = [];

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: pieData,
      width: this.data.windowWidth,
      height: 300,
      dataLabel: true,
    })

    for (var i = 0; i < 10; i++) {
      categories.push('2016-9-' + (i + 1));
    }
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: categories,
      animation: true,
      // background: '#f5f5f5',
      series: lineData,
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: 'NUM',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: this.data.windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });

  },


  onTabClick(e: any) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })


  },
  updateButton0(e: any) {
    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: pieData,
      width: this.data.windowWidth,
      height: 300,
      dataLabel: true,
    })


    console.log("press button0");
    lo

    wx.request({
      url:"http://localhost:9090/record/data/total?whid=" + app.globalData.gWarehouse + "&minTime=" + this.data.startDate + " 00:00:00",
      data: {
        minTime: this.data.startDate,
        whid: app.globalData.gWarehouse
      },
      success(res) {
        console.log(res);

      }
    })


  },

  updateButton2(e: any) {
    console.log("press button2");
    var t="http://localhost:9090/record/data/total?whid=" + app.globalData.gWarehouse + "&minTime=" + this.data.startDate + " 00:00:00"
    console.log(t);
    wx.request({
      url:"http://localhost:9090/record/data/total?whid=" + app.globalData.gWarehouse + "&minTime=" + this.data.startDate +"+00:00:00",
      method:"POST",
      data: {
        minTime: this.data.startDate,
        whid: app.globalData.gWarehouse
      },
      
      success(res) {
        var set=res.data
        console.log(set);
        console.log(set["1"]);
        
        var timeset = set["1"];
        var numset = set["2"];
        console.log(res);
       console.log(timeset.length);
       
        for (var i = 0; i < timeset.length; i++) {
          timeset[i] = timeset[i].substr(11,11);
          console.log(timeset[i]);
        }
        lineChart.updateData({
          categories: timeset,
          series: [{
            name: "warehouse",
            data: numset,

          }]
        });


      }
    })


  },

  updata0() {
    pieChart.updateData({

    })
  },

  updata2() {
    lineChart.updateData({
    })
  },

  dateFormat() {



  },


  onChange(e: any) {
    console.log("change");

    const index = e.detail.index
    this.setData({
      activeTab: index
    })

    if (index == 0) {
      this.updata0()
    } else if (index == 1) {

    } else if (index == 2) {
      this.updata2()
    }





  },

  handleClick(e: any) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})